//
//  VisitorView.swift
//  WeiBo
//
//  Created by Gao on 16/3/2.
//  Copyright © 2016年 Gao. All rights reserved.
//

import UIKit

/**
 *  在VisitorView上对登录\注册按钮的点击进行回调的协议
 *  如果不是实现NSObjectProtocol,将不能使用在对象身上
 */
protocol VisitorViewDelegate:NSObjectProtocol
{
    func loginButtonWillClick()
    
    func registerButtonWillClick()
}


class VisitorView: UIView
{
    weak var delegate:VisitorViewDelegate?
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        setupUI()
    }
    
    //通过story board或者xib创建会调用这个方法
    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }

    /**
     背景图片的设置
     */
    func setupUI()
    {
        
        addSubview(IconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //布局
        //1.转盘居中
        IconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        
        //2.房子居中
        homeIconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        
        //3.标签
        messageLabel.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: IconView, size: nil)
        
        //哪个空间的什么属性 等于／大于／小于 另一个控件的什么属性 乘于多少  加上多少
        let Layout = NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224)
        addConstraint(Layout)
        
        //注册按钮
        registerButton.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 35),offset: CGPoint(x: 0, y: 20))
        
        //登录按钮
        loginButton.xmg_AlignVertical(type:  XMG_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 35), offset: CGPoint(x:0,y:20))
        
        //遮盖视图
        maskIconView.xmg_Fill(self)
    }
    // 转盘背景
    lazy var IconView:UIImageView =
    {
       let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
       return imageView
    }()
    
    // 房子背景
    lazy var homeIconView:UIImageView =
    {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return imageView
    }()
    
    //标签
    lazy var messageLabel:UILabel =
    {
       let label = UILabel()
        label.text = "asdasdasdasadasdasdasdasdasdasdasadasdasdasdasdasdasdasadasdasdasd"
        label.numberOfLines  = 0
        return label
    }()
    
    //登录按钮
    lazy var loginButton:UIButton =
    {
        let btn = UIButton()
            btn.setTitle("登陆", forState: .Normal)
            btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
            btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
            btn.addTarget(self, action: Selector("loginButtonClick"), forControlEvents: .TouchUpInside)
        return btn
    }()
    
    //注册按钮
    lazy var registerButton:UIButton =
    {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.addTarget(self, action: Selector("registerButtonClick"), forControlEvents: .TouchUpInside)
        return btn
    }()
    
    //遮盖视图
    lazy var maskIconView:UIImageView =
    {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return imageView
    }()
    
    //登录按钮响应事件
    func loginButtonClick()
    {
        delegate?.loginButtonWillClick()
    }
    
    //注册按钮的点击事件
    func registerButtonClick()
    {
        delegate?.registerButtonWillClick()
    }
}
